Try to untangle the uncodings in gdb. I'm not really sure if I made it better
authorrobertlipe@gmail.com <robertlipe@gmail.com@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 1 Jan 2014 23:25:38 +0000 (23:25 +0000)
committerrobertlipe@gmail.com <robertlipe@gmail.com@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Wed, 1 Jan 2014 23:25:38 +0000 (23:25 +0000)
or worse...This is not one of our formats that makes me proud.

git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4702 f51c46e8-681c-474f-0cfe-069cfd0219fb

gpsbabel/easygps.cc
gpsbabel/gbfile.cc
gpsbabel/gbfile.h
gpsbabel/gdb.cc
gpsbabel/mapsource.cc

index c446aa9ae26e3cc8db87852134655620415a65f9..7c25eb0aeb97944895194baee3092503a71b918b 100644 (file)
@@ -112,19 +112,13 @@ data_read(void)
         wpt_tmp->notes = gbfgetcstr(file_in);
         break;
       case 9: { /* NULL Terminated (vs. pascal) link */
-        char* url = gbfgetcstr(file_in);
+        QString url = gbfgetcstr(file_in);
         link.url_ = url;
-        if (url) {
-          xfree(url);
-        }
       }
       break;
       case 0x10: {
-        char* ult = gbfgetcstr(file_in);
+        QString ult = gbfgetcstr(file_in);
         link.url_link_text_ = ult;
-        if (ult) {
-          xfree(ult);
-        }
       }
       break;
       case 0x63:
index 60ffaaa1423354646ee2f20169a1038100b5df66..1b5d3f5422ac161b48c98367a2182b82430abca7 100644 (file)
@@ -981,7 +981,7 @@ gbfgetflt(gbfile* file)
  */
 
 char*
-gbfgetcstr(gbfile* file)
+gbfgetcstr_old(gbfile* file)
 {
   char* result;
   int len = 0;
@@ -1011,6 +1011,15 @@ gbfgetcstr(gbfile* file)
   return result;
 }
 
+QString
+gbfgetcstr(gbfile* file)
+{
+  char* result = gbfgetcstr_old(file);
+  QString rv(result);
+  xfree(result);
+  return rv; 
+}
+
 /*
  * gbfgetpstr: Reads a pascal string (first byte is length) from file.
  *             The result is a temporary allocated entity: use it or free it!
index 72c1bde5c40883a4a8669152cb1d1b1170f0cc73..669dd2073f191786404ceadacdc0c821abfa162d 100644 (file)
@@ -123,8 +123,9 @@ int16_t gbfgetint16(gbfile* file);
 double gbfgetdbl(gbfile* file);                        // read a double value
 float gbfgetflt(gbfile* file);                 // read a float value
 char* gbfgetstr(gbfile* file);                 // read until any type of line-breaks or EOF
-QString gbfgetpstr(gbfile* file);                      // read a pascal string
-char* gbfgetcstr(gbfile* file);                        // read a null terminated string
+QString gbfgetpstr(gbfile* file);              // read a pascal string
+QString gbfgetcstr(gbfile* file);              // read a null terminated string
+char* gbfgetcstr_old(gbfile* file);            // read a null terminated string
 
 int gbfputint16(const int16_t i, gbfile* file);
 #define gbfputuint16(a,b) gbfputint16((uint16_t)(a),(b))
index e36bec8966f05ba1c1a7cdb966d161195f69da31..af9cf1a24e547b2ae3669747603b50f9562278f5 100644 (file)
@@ -209,18 +209,41 @@ disp_summary(const gbfile* f)
 #define FREAD(a,b) gbfread(a,(b),1,fin)
 #define FREAD_i32 gbfgetint32(fin)
 #define FREAD_i16 gbfgetint16(fin)
-#define FREAD_STR(a) gdb_fread_str(a,sizeof(a),fin)
-#if NEW_STRINGS
-#define FREAD_CSTR \
-  (gdb_ver >= GDB_VER_UTF8) ? QString::fromUtf8(gdb_fread_cstr(fin)) : \
-  QString::fromLatin1(gdb_fread_cstr(fin))
-#else
-#define FREAD_CSTR gdb_fread_cstr(fin)
-#endif
-#define FREAD_CSTR_AS_QSTR gdb_fread_cstr_as_qstr(fin)
 #define FREAD_DBL gbfgetdbl(fin)
 #define FREAD_LATLON GPS_Math_Semi_To_Deg(gbfgetint32(fin))
 
+#define FREAD_STR(a) gdb_fread_str(a,sizeof(a),fin)
+
+// This is all very messy.  Some versions of GDB store strings as 
+// 8859-1 strings and others as UTF8.  This wrapper tries to hide 
+// all that while (while keeping the character sets correct) and
+// not pushing that decision  down into gbfread.  This module is
+// still pretty messy and the points as to which fields are encode
+// which ways in which versions are not at all clear, leaing to 
+// encoding issues on read and leaks because of teh differences 
+// in calling conventions on who owns/destroys the result.
+//#define FREAD_CSTR \
+//  (gdb_ver >= GDB_VER_UTF8) ? QString::fromUtf8(gdb_fread_cstr(fin)) : \
+//  QString::fromLatin1(gdb_fread_cstr(fin))
+#define FREAD_CSTR_AS_QSTR gbfgetcstr(fin)
+
+static char* gdb_fread_cstr(gbfile* fin);
+
+QString fread_cstr()
+{
+  QString rv;
+  char* s = gdb_fread_cstr(fin);
+  if (gdb_ver >= GDB_VER_UTF8) { 
+    rv = QString::fromUtf8(s);
+  } else {
+    rv = QString::fromLatin1(s);
+  }
+  
+  xfree(s);
+
+  return rv;
+}
+
 #if GDB_DEBUG
 static char*
 nice(const char* str)
@@ -257,22 +280,14 @@ nice(const char* str)
 static char*
 gdb_fread_cstr(gbfile* fin)
 {
-  char* result = gbfgetcstr(fin);
+  char* result = gbfgetcstr_old(fin);
 
   if (result && (*result == '\0')) {
     xfree(result);
     result = NULL;
   }
-  return result;
-}
 
-static QString
-gdb_fread_cstr_as_qstr(gbfile* fin)
-{
-  char* result = gdb_fread_cstr(fin);
-  QString qresult = result;
-  xfree(result);
-  return qresult;
+  return result;
 }
 
 static int
@@ -303,21 +318,10 @@ gdb_fread_strlist(void)
   count = FREAD_i32;
 
   while (count > 0) {
-#if HUH
-    char* str = FREAD_CSTR;
-    if (str != NULL) {
-      if (*str && (res == NULL)) {
-        res = str;
-      } else {
-        xfree(str);
-      }
-    }
-#else
-    QString str = FREAD_CSTR;
+    QString str = fread_cstr();
     if (!str.isEmpty()) {
       res = str;
     }
-#endif
     count--;
   }
 
@@ -555,7 +559,7 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out)
 
   gmsd = garmin_fs_alloc(-1);
   fs_chain_add(&res->fs, (format_specific_data*) gmsd);
-  res->shortname = FREAD_CSTR;
+  res->shortname = fread_cstr();
 #if GDB_DEBUG
   sn = xstrdup(nice(res->shortname));
 #endif
@@ -598,7 +602,7 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out)
          res->latitude < 0 ? 'S' : 'N', res->latitude,
          res->longitude < 0 ? 'W' : 'E', res->longitude);
 #endif
-  res->notes = FREAD_CSTR;
+  res->notes = fread_cstr();
 #if GDB_DEBUG
   DBG(GDB_DBG_WPTe, res->notes) {
     char* str = gstrsub(res->notes, "\r\n", ", ");
@@ -691,7 +695,7 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out)
     GMSD_SETSTR(addr, bufp);
 
     FREAD(buf, 5);                             /* instruction depended */
-    res->description = FREAD_CSTR;             /* instruction */
+    res->description = FREAD_CSTR_AS_QSTR;     /* instruction */
     url_ct = FREAD_i32;
     for (i = url_ct; (i); i--) {
       QString str = FREAD_CSTR_AS_QSTR;
@@ -801,7 +805,7 @@ read_route(void)
   warnings = 0;
 
   rte = route_head_alloc();
-  rte->rte_name = FREAD_CSTR;
+  rte->rte_name = fread_cstr();
   FREAD(buf, 1);                       /* display/autoname - 1 byte */
 
   if (FREAD_C == 0) {          /* max. data flag */
@@ -838,7 +842,7 @@ read_route(void)
     wpt = waypt_new();
     rtept_ct++;
 
-    wpt->shortname = FREAD_CSTR;       /* shortname */
+    wpt->shortname = fread_cstr();     /* shortname */
     wpt_class = FREAD_i32;             /* waypoint class */
     FREAD_STR(buf);                    /* country code */
     FREAD(buf, 18 + 4);                /* subclass part 1-3 / unknown */
@@ -979,7 +983,7 @@ read_route(void)
 
   /* VERSION DEPENDENT CODE */
   if (gdb_ver <= GDB_VER_2) {
-    rte->rte_url = FREAD_CSTR_AS_QSTR;
+    rte->rte_url = fread_cstr();
   } else {
     rte->rte_url = gdb_fread_strlist();
 
@@ -987,7 +991,7 @@ read_route(void)
     rte->line_color.bbggrr = gt_color_value(color_idx);
     FREAD(buf, 1);                     /* ?????????????????????????????????? */
 
-    rte->rte_desc = FREAD_CSTR;
+    rte->rte_desc = fread_cstr();
 #if 0
     /* replace CRLF's with ", " */
     if (rte->rte_desc) {
@@ -1015,7 +1019,7 @@ read_track(void)
   trk_ct++;
 
   res = route_head_alloc();
-  res->rte_name = FREAD_CSTR;
+  res->rte_name = fread_cstr();
 //     res->rte_num = trk_ct;
 
   FREAD(&dummy, 1);            /* display - 1 byte */
index ab8acb849eeff7bb1bfdac6ea9b86be970587545..4ceb34b09680947cf42f8ecdc17958c480b471a5 100644 (file)
@@ -551,7 +551,7 @@ mps_waypoint_r(gbfile* mps_file, int mps_ver, waypoint** wpt, unsigned int* mpsc
     gbfseek(mps_file, 8, SEEK_CUR);
   }
 
-  QScopedPointer<char, QScopedPointerPodDeleter>wptdesc (gbfgetcstr(mps_file));
+  QString wptdesc = gbfgetcstr(mps_file);
 
   if (gbfgetc(mps_file) == 1) {                                /* proximity validity */
     mps_proximity = gbfgetdbl(mps_file);
@@ -579,14 +579,13 @@ mps_waypoint_r(gbfile* mps_file, int mps_ver, waypoint** wpt, unsigned int* mpsc
 
   if ((mps_ver == 4) || (mps_ver == 5)) {
     gbfread(tbuf, 6, 1, mps_file);                             /* unknown */
-    QScopedPointer<char, QScopedPointerPodDeleter>wptnotes (gbfgetcstr(mps_file));
-    thisWaypoint->notes = wptnotes.take();
+    thisWaypoint->notes = gbfgetcstr(mps_file);
   } else {
     gbfread(tbuf, 2, 1, mps_file);                             /* unknown */
   }
 
   thisWaypoint->shortname = wptname;
-  thisWaypoint->description = wptdesc.take();
+  thisWaypoint->description = wptdesc;
   thisWaypoint->latitude = GPS_Math_Semi_To_Deg(lat);
   thisWaypoint->longitude = GPS_Math_Semi_To_Deg(lon);
   thisWaypoint->altitude = mps_altitude;
@@ -914,7 +913,7 @@ mps_route_r(gbfile* mps_file, int mps_ver, route_head** rte)
   double       mps_altitude = unknown_alt;
   double       mps_depth = unknown_alt;
 
-  QScopedPointer<char, QScopedPointerPodDeleter>rtename(gbfgetcstr(mps_file));
+  QString rtename = gbfgetcstr(mps_file);
 #ifdef MPS_DEBUG
   fprintf(stderr, "mps_route_r: reading route %s\n", rtename);
 #endif
@@ -959,7 +958,7 @@ mps_route_r(gbfile* mps_file, int mps_ver, route_head** rte)
 #endif
 
   rte_head = route_head_alloc();
-  rte_head->rte_name = rtename.take();
+  rte_head->rte_name = rtename;
   route_add_head(rte_head);
   *rte = rte_head;
 
@@ -1541,7 +1540,7 @@ mps_track_r(gbfile* mps_file, int mps_ver, route_head** trk)
 
   (void)mps_ver;
 
-  QScopedPointer<char, QScopedPointerPodDeleter>trkname (gbfgetcstr(mps_file));
+  QString trkname = gbfgetcstr(mps_file);
 #ifdef MPS_DEBUG
   fprintf(stderr, "mps_track_r: reading track %s\n", trkname);
 #endif
@@ -1563,7 +1562,7 @@ mps_track_r(gbfile* mps_file, int mps_ver, route_head** trk)
 #endif
 
   track_head = route_head_alloc();
-  track_head->rte_name = trkname.take();
+  track_head->rte_name = trkname;
   track_add_head(track_head);
   *trk = track_head;